import random
from view.endSrc.MixedGaussianDatasetGenerator import MixedGaussianDatasetGenerator
from view.endSrc.MySqlConn import MySqlConn
from view.endSrc.DBConfig import DBConfig

from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
import numpy as np

# def visualization(meanList, dataset, labels):
#
#     '''
#     This method conduct the dimensionality reduction process by principle component analysis,
#     which transform the data to 2D data, and plot the 2D data and centers by clusters.
#
#     Examples:
#     --------
#     dataset.visualization()
#     '''
#
#     k = len(meanList)
#     dataACen = np.concatenate((meanList, dataset), axis=0)
#     pca = PCA(n_components=2)
#     pca.fit(dataACen)
#     dataACen2d = pca.fit_transform(dataACen)
#     Center2d = dataACen2d[:k ,:]
#     data2d = dataACen2d[k: ,:]
#     x = data2d[: ,0]
#     y = data2d[: ,1]
#     plt.scatter(x ,y ,c=labels ,marker="+")
#     for i in range(k):
#         plt.plot(Center2d[i ,0], Center2d[i ,1], c = 'r', marker = 'x')
#         text = "c" + str( i +1)
#         plt.annotate(text, xy = (Center2d[i ,0], Center2d[i ,1]),
#                      xytext = (Center2d[i ,0 ] +1.5, Center2d[i ,1 ] +1.5))
#
#     plt.xlabel('dimension 1')
#     plt.ylabel('dimension 2')
#     plt.title('Principle Component Analysis to 2D data')
#     plt.show()
#
#
# def visual2D(meanList, dataset, labels):
#
#     k = len(meanList)
#     plt.scatter(dataset[: ,0], dataset[: ,1],
#                 label='2D origin', c=labels, marker="+")
#     for i in range(k):
#
#         plt.plot(meanList[i][0], meanList[i][1], c ='r', marker ='x')
#         text = "c" + str( i +1)
#         plt.annotate(text, xy = (meanList[i][0], meanList[i][1]),
#                      xytext = (meanList[i][0], meanList[i][1]))
#
#
#     plt.xlabel('x')
#     plt.ylabel('y')
#     plt.title('2D origin data')
#     plt.legend()
#     plt.show()



def genMixedGaussDataAndSaveDB(sqlConn):
    comp = 30
    numOfFeatures = 2
    numOfInstances = 30000
    overlap = 1.5
    xRange = [10, 500]
    des = 'big dataset'
    name = 'b1'


    ps = []
    for i in range(comp):
        ps.append(random.uniform(1, 3))

    print(ps)
    g = MixedGaussianDatasetGenerator(numOfInstances=numOfInstances, numOfFeatures=numOfFeatures,
                                      overlap=overlap,
                                      paiList=ps,
                                      xRange=xRange)

    g.genSeparableComponents()
    mDataset = g.genMixedDataset()
    mDataset.setDescription(des)

    mDataset.t.setSqlConn(sqlConn)
    mDataset.t.createTable()
    mDataset.saveToDB(sqlConn, name)


if __name__ == '__main__':
    sqlConn = MySqlConn(DBConfig())
    genMixedGaussDataAndSaveDB(sqlConn)
